package android.tibus.testproject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.*;

public class TestProject extends Activity
{
	static final int DIALOG_ADD_NODE = 1;
	static final int DIALOG_DELETE_NODE = 2;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId())
    	{
    		case R.id.exit:
    			exitDialog();
    			return true;
    			
    		case R.id.add_node:
    			addNode();
    			return true;
    			
    		case R.id.delete_node:
    			deleteNode();
    			return true;
    			
    		case R.id.list_nodes:
    			listNodes();
    			return true;
    			
			default:
				return true;

    	}
    }
    
    @Override
    protected Dialog onCreateDialog(int id)
    {
    	Dialog dialog;
    	
    	switch(id)
    	{
	    	case DIALOG_ADD_NODE:
	    		dialog = new Dialog(this);
	    		dialog.setTitle("Adding Node");
	    		dialog.show();
	    		break;
	    		
	    	case DIALOG_DELETE_NODE:
	    		dialog = new Dialog(this);
	    		dialog.setTitle("Deleting Node");
	    		dialog.show();
	    		break;
	    		
    		default:
    			
    			dialog = null;
    			
    	}
    	
    	return dialog;
    }
    
    public void listNodes()
    {
        setContentView(R.layout.node_list);
    }
    
    private void addNode()
    {
    	showDialog(DIALOG_ADD_NODE);
    }
    
    private void deleteNode()
    {
    	showDialog(DIALOG_DELETE_NODE);
    }
    
    private void exitDialog()
    {
    	AlertDialog.Builder exitBuilder = new AlertDialog.Builder(this);
    	exitBuilder.setMessage("Are you sure you want to exit?");
    	exitBuilder.setCancelable(false);
    	exitBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int id) {
    			// Action for yes
    			TestProject.this.finish();
    		}
    	});
    	
    	exitBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int id) {
    			// Action for no
    			dialog.cancel();
    		}
    	});
    	
    	AlertDialog alert = exitBuilder.create();
    	
    	alert.setTitle("Exit?");
    	alert.setIcon(R.drawable.icon);
    	alert.show();
    }
    
    /*
    @Override
    public boolean onCreateContextMenu(Menu menu)
    {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    */
}